function e(e, n) {
  if (!(e instanceof n)) throw new TypeError("Cannot call a class as a function");
}

Object.defineProperty(exports, "__esModule", {
  value: !0
});

var n = function() {
    function e(e, n) {
      for (var t = 0; t < n.length; t++) {
        var o = n[t];
        o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0),
          Object.defineProperty(e, o.key, o);
      }
    }
    return function(n, t, o) {
      return t && e(n.prototype, t), o && e(n, o), n;
    };
  }(),
  t = function() {
    function t(n) {
      e(this, t),
        this.type = n;
    }
    return n(t, [{
      key: "download",
      value: function(e) {
        var n = this;
        return new Promise(function(t, o) {
          n._startDownloading(),
            n._getAllTempFile(e).then(function(e) {
              return n._saveAllTempFiles(e);
            }).then(function(e) {
              n._end("已保存相册!"), t(e);
            }).catch(function(e) {
              var t = "下载失败";
              "saveVideoToPhotosAlbum:fail auth deny" != e.errMsg && "saveImageToPhotosAlbum:fail auth deny" != e.errMsg || (e.authDeny = !0, t = "未授权!"),
                n._end(t),
                wx.hideLoading();
                o(e);
            });
        });
      }
    }, {
      key: "_saveAllTempFiles",
      value: function(e) {
        var n = this;
        return new Promise(function(t, o) {
          var i = [];
          e.forEach(function(e, t) {
              i.push(n._saveFile(e.tempFilePath));
            }),
            Promise.all(i).then(function(e) {
              t(e);
            }).catch(function(e) {
              o(e);
            });
        });
      }
    }, {
      key: "_getAllTempFile",
      value: function(e) {
        var n = this;
        return new Promise(function(t, o) {
          for (var i = [], a = 0; a < e.length; a++)
            i.push(n._getDownloadPromise(e[a]));
          Promise.all(i).then(function(e) {
            t(e);
          }).catch(function(e) {
            o(e);
          });
        });
      }
    }, {
      key: "_startDownloading",
      value: function() {
        wx.showLoading({
          title: "正在下载中",
          mask: !0
        });
      }
    }, {
      key: "_end",
      value: function(e) {
        wx.showLoading({
          title: e
        }), setTimeout(function() {
          wx.hideLoading();
        },1000);
      }
    }, {
      key: "_saveFile",
      value: function(e) {
        var n = this;
        return new Promise(function(t, o) {
          var i = {
            filePath: e,
            success: function(e) {
              t(e);
            },
            fail: function(e) {
              o(e);
            }
          };
          switch (n.type) {
            case "video":
              wx.saveVideoToPhotosAlbum(i);

            case "image":
              wx.saveImageToPhotosAlbum(i);
          }
        });
      }
    }, {
      key: "_getDownloadPromise",
      value: function(e) {
        return new Promise(function(n, t) {
          wx.downloadFile({
            url: e,
            success: function(e) {
              n(e);
            },
            fail: function(e) {
              t(e);
            }
          });
        });
      }
    }]), t;
  }();

exports.DownloadHelper = t;